<?php

/**
 * This is the model class for table "{{material_model}}".
 *
 * The followings are the available columns in table '{{material_model}}':
 * @property integer $id
 * @property integer $materialId
 * @property string $model
 * @property string $code
 * @property string $unit
 * @property string $statePrice
 * @property string $referenceCode
 * @property string $note
 *
 * The followings are the available model relations:
 * @property Material $material
 */
class MaterialModel extends CActiveRecord {
	/**
	 * Returns the static model of the specified AR class.
	 * @return MaterialModel the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return '{{material_model}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array(
				'unit',
				'required',
			),
			array(
				'materialId+model',
				'components.db.UniqueMultiColumnValidator',
				'errorAttributes' => array(
					'model'
				),
			),
			array(
				'model, code, unit, referenceCode, note',
				'length',
				'max' => 255
			),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array(
				'id, materialId, model, code, unit, referenceCode, note',
				'safe',
				'on' => 'search'
			),
		);
	}

	public function behaviors() {
		return array(
			'AuditableBehavior' => array(
				'class' => 'components.db.AuditableBehavior',
				'attributes' => 'materialId, code, model, note, referenceCode, unit'
			),
			'LogRecentBehavior' => array(
				'class' => 'components.db.LogRecentBehavior',
				'logRelation' => 'material',
			),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'material' => array(
				self::BELONGS_TO,
				'Material',
				'materialId'
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'id' => 'ID',
			'materialId' => '材料名称',
			'model' => '型号',
			'code' => '清单编号',
			'unit' => '标准单位',
			'referenceCode' => '配合/台班号',
			'note' => '备注',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search() {
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria = new CDbCriteria;

		$criteria->compare('id', $this->id);
		$criteria->compare('materialId', $this->materialId);
		$criteria->compare('model', $this->model, true);
		$criteria->compare('code', $this->code, true);
		$criteria->compare('unit', $this->unit, true);
		$criteria->compare('referenceCode', $this->referenceCode, true);
		$criteria->compare('note', $this->note, true);

		return new CActiveDataProvider($this, array(
			'criteria' => $criteria,
		));
	}
	/**
	 * 
	 * @param int $materialId
	 * @return MaterialModel
	 */
	public function withMaterial($materialId) {
		$this->getDbCriteria()
			->mergeWith(
				array(
					'condition' => 'materialId=:materialId',
					'params' => array(
						':materialId' => $materialId
					),
				));
		return $this;
	}

	public function getModelName() {
		return $this->material->name . ' ' . ($this->model ? $this->model : '(无)') 
//		. ' ('. $this->unit . ')'
		;
	}
}
